#!/usr/bin/env python
# _*_coding:utf-8 _*_
__author__ = "Gao"

import random,math,pygame
import sys

from pygame.locals import *

from pygame.sprite import Sprite




class MySprite(Sprite):
    '''自制精灵，实现动画'''
    def __init__(self,target):
        super(MySprite, self).__init__()
        self.master_image = None
        self.frame = 0
        self.old_frame = -1
        self.frame_width = 1
        self.frame_height = 1
        self.first_frame = 0
        self.last_frame = 0
        self.columns = 1
        self.last_time = 0

    @property    # 封装当前帧的x坐标
    def x(self):
        return self.rect.x
    @x.setter
    def x(self,value):
        self.rect.x = value

    @property
    def y(self):    # 封装当前帧的y坐标
        return self.rect.y
    @y.setter
    def y(self,value):
        self.rect.y = value

    @property
    def position(self):    # 封装当前帧的x、y坐标
        return self.rect.topleft
    @position.setter
    def position(self, pos):
        self.rect.topleft = pos

    def load(self,filename,width,height,columns):
        '''载入序列图
        args:
            filename: 序列图
            width: 每帧宽度
            height: 每帧高度
            columns: 该序列图一共有几列
        '''
        self.master_image = pygame.image.load(filename).convert_alpha()    # 主图像
        self.frame_width = width    # 每帧宽度
        self.frame_height = height    # 每帧高度
        self.rect = Rect(0,100,width,height)    # 当前帧显示在屏幕的哪个位置
        self.columns = columns    # 图像一共有几列
        rect = self.master_image.get_rect()
        self.last_frame = (rect.width // width) * (rect.height // height) - 1    # 计算序列图最后一帧是第几帧

    def update(self,current_time,rate=30):
        '''绘制精灵'''
        if current_time > self.last_time + rate:    # 间隔多少毫秒更新帧
            self.frame += 1
            if self.frame > self.last_frame:    # 循环到第一帧
                self.frame = self.first_frame
            self.last_time = current_time

        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns) * self.frame_width    # 计算当前帧位于序列图左上角的x位置
            frame_y = (self.frame // self.columns) * self.frame_height    # 计算当前帧位于序列图左上角的y的位置
            rect = Rect(frame_x, frame_y , self.frame_width, self.frame_height)    # 获取当前帧位于序列图的位置
            self.image = self.master_image.subsurface(rect)    # 使用当前帧位置截取当前帧图像
            self.old_frame = self.frame

    def __str__(self):
        return str(self.frame) + ',' + str(self.first_frame) + ',' + str(self.last_frame) + ',' + str(self.frame_width) + ',' + str(self.frame_height) + ',' + str(self.columns) + ',' + str(self.rect)


def print_text(font,x,y,text,color=(255,255,255)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x,y))

pygame.init()
screen = pygame.display.set_mode((800,600),0 ,32)    # 使用32位颜色
font = pygame.font.Font(None,18)

framerate = pygame.time.Clock()

cat = MySprite(screen)
cat.load('test.png',100,100,4)



group = pygame.sprite.Group()    # 创建精灵组
group.add(cat)    # 把精灵添加到精灵组中


while True:
    framerate.tick(30)    # 设定游戏的帧数，相当于每秒循环多少次
    ticks = pygame.time.get_ticks()    # 获取当前运行时间(毫秒级)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    key = pygame.key.get_pressed()
    if key[pygame.K_ESCAPE]:
        sys.exit()

    screen.fill((0,0,100))
    group.update(ticks)    # 精灵组中的所有精灵执行update方法
    group.draw(screen)    # 精灵组中的所有精灵执行draw方法
    pygame.display.update()